package controlador.ventas;

import controlador.ServletHelper;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.simple.JSONObject;
import persistencia.BoletaDetalleComposite;
import persistencia.DetalleBoletaBean;

@WebServlet(name = "EditarCantidad", urlPatterns = {"/EditarCantidad"})
public class EditarCantidad extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conexion = null;
        boolean condi = ServletHelper.validarUsu(request, response);
        if (condi) {

            int cantidad = 1;

            String codProd = (request.getParameter("COD_PRODUCTO"));
            String strCantidad = (request.getParameter("CANTIDAD"));
            if (strCantidad != null) {
                cantidad = Integer.parseInt(request.getParameter("CANTIDAD"));
            }
            HttpSession session = request.getSession();
            BoletaDetalleComposite bdc;
            bdc = (BoletaDetalleComposite) session.getAttribute("BOLETA_DETALLE");
            JSONObject json = new JSONObject();
            json.put("OK", "OK");
            out.print(json);

            DetalleBoletaBean detBolBean = new DetalleBoletaBean(conexion);
            bdc.setListaDetalle(detBolBean.editarCantidad(bdc.getListaDetalle(), codProd, cantidad));
            session.setAttribute("BOLETA_DETALLE", bdc);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
